<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Question extends CI_Controller {
	
	public function index() { }

	// public function getQuestion($vId) {
	// 	$this->load->model('Model_question');
	// 	$cursor = $this->Model_question->getQuestion($vId);

	// 	// Conver mongoCursor to JSON
	// 	echo json_encode($cursor);	
	// }

	// public function addQuestion() {
	// 	$v = $this->input->post('data');
	// 	$this->load->model('Model_question');

	// 	$cc = $this->Model_question->insert_question($v['_id'], $v['cat'], $v['description']);
	// 	print_r($cc);
	// }
	
	public function getQuestions() {
		$idArr = $this->input->post('arr');
		$this->load->model('Model_question');

		if($idArr) {
			$cursor = $this->Model_question->getQuestionsArr($idArr);
			$cursor->sort(array('question' => 1));
			$arr = array();
			foreach (iterator_to_array($cursor) as $k) {
				array_push($arr, $k);
			}
			echo json_encode($arr);
			// print_r($idArr);
		}
		else {
			$cursor = $this->Model_question->getQuestions();
			$cursor->sort(array('question' => 1));
			$arr = array();
			foreach (iterator_to_array($cursor) as $k) {
				array_push($arr, $k);
			}
			echo json_encode($arr);
			return;
		}
	}

	public function addQuestion() {
		$q = $this->input->post('data');
		$this->load->model('Model_question');

		$cc = $this->Model_question->insert_question($q['question'], $q['cat'], $q['answer']);
		print_r($cc);
	}

	// =====================================================================================================================
	// =====================================================================================================================
	// =====================================================================================================================

	public function addOptQuestion() {
		$q = $this->input->post('data');
		$this->load->model('Model_question');
		$cc = $this->Model_question->insert_opt_question($q);
		echo $cc;
	}

}
